<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\ProductArea;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;
use yii\helpers\ArrayHelper;

class Up
{
    protected $order;
    protected $user;

    protected $set;

    protected $first_order_item;

    // 只处理会员这个升级
    public function __construct(Order $order)
    {
        $set = SetUserUp::getLastOne();

        $user = User::findOne(['id' => $order->user_id]);
        // if ($user->level < UserLevel::$keys['member']) {
        //     $amount = $order->total_pay - $order->total_freight;
        //     $set = SetUserUp::getLastOne();
        //     if ($set->member <= $amount) {
        //         $user->level = UserLevel::$keys['member'];
        //         $user->save();
        //     }
        // }

        // v1 必须是 "运营商 - 会员"
        $r = Chain::getReferrer($user->id, true);
        if ($r && $r->level == UserLevel::$keys['member']) {
            $count = User::find()
                ->where(['>=', 'level', UserLevel::$keys['member']])
                ->andWhere(['referrer_id' => $r->id])
                ->count();
            if ($count >= $set->vip1) {
                $r->level = UserLevel::$keys['vip1'];
                $r->save();
            }
        }

        // 本人的数量
        // $count = (int)Order::find()
        //     ->where(['user_id' => $user->id])
        //     ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
        //     ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
        //     ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
        //     ->andWhere(['!=', 'status', Order::$statusMapping['REFUND']])
        //     ->sum('total_quantity');

        // var_dump($count);exit;
        $chain = Chain::getReferrerChain($order->user_id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            UserLevel::$keys['vip3'],
            UserLevel::$keys['vip4'],
            UserLevel::$keys['svip1'],
        ], true);
        $chain = array_merge([$user], $chain);
        foreach ($chain as $user) {
            $this->chainUp($user);
        }


        // v2 - v4
        // $chain = Chain::getReferrerChain($order->user_id, [
        //     UserLevel::$keys['vip1'],
        //     UserLevel::$keys['vip2'],
        //     UserLevel::$keys['vip3'],
        //     UserLevel::$keys['vip4'],
        // ], true);
        //
        //
        // foreach ($chain as $u) {
        //
        //     $list = Chain::getChildrenAll($u->id);
        //     $ids = ArrayHelper::getColumn($list, 'id');
        //     $ids[] = $u->id;
        //     $count = (int)Order::find()
        //         ->where(['user_id' => $ids])
        //         ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
        //         ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
        //         ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
        //         ->andWhere(['!=', 'status', Order::$statusMapping['REFUND']])
        //         ->sum('total_quantity');
        //
        //     // var_dump($ids, $count);exit;
        //
        //     if ($u->level < UserLevel::$keys['vip4']) {
        //         if ($count >= $set->vip4) {
        //             $u->level = UserLevel::$keys['vip4'];
        //             $u->save();
        //         }
        //     }
        //     if ($u->level < UserLevel::$keys['vip3']) {
        //         if ($count >= $set->vip3) {
        //             $u->level = UserLevel::$keys['vip3'];
        //             $u->save();
        //         }
        //     }
        //     if ($u->level < UserLevel::$keys['vip2']) {
        //         if ($count >= $set->vip2) {
        //             $u->level = UserLevel::$keys['vip2'];
        //             $u->save();
        //         }
        //     }
        //
        //     // svip
        //     if ($u->level < UserLevel::$keys['svip1']) {
        //         if ($count >= $set->svip1) {
        //             $u->level = UserLevel::$keys['svip1'];
        //             $u->save();
        //         }
        //     }
        //
        //     if ($u->level < UserLevel::$keys['svip2']) {
        //         $count = User::find()
        //             ->where(['>=', 'level', UserLevel::$keys['svip1']])
        //             ->andWhere(['referrer_id' => $r->id])
        //             ->count();
        //         if ($count >= $set->svip2) {
        //             $u->level = UserLevel::$keys['svip2'];
        //             $u->save();
        //         }
        //     }
        // }
    }

    private function chainUp($user)
    {
        $set = SetUserUp::getLastOne();

        $list = Chain::getChildrenAll($user->id);
            $ids = ArrayHelper::getColumn($list, 'id');
            // 本人的不算
            // $ids[] = $user->id;
            $count = (int)Order::find()
                ->where(['user_id' => $ids])
                ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
                ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
                ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
                ->andWhere(['!=', 'status', Order::$statusMapping['REFUND']])
                ->sum('total_quantity');

        // v2 - v4
        if ($user->level < UserLevel::$keys['vip4']) {
            if ($count >= $set->vip4) {
                $user->level = UserLevel::$keys['vip4'];
                $user->save();
            }
        }

        if ($user->level < UserLevel::$keys['vip3']) {
            if ($count >= $set->vip3) {
                $user->level = UserLevel::$keys['vip3'];
                $user->save();
            }
        }

        if ($user->level < UserLevel::$keys['vip2']) {
            if ($count >= $set->vip2) {
                $user->level = UserLevel::$keys['vip2'];
                $user->save();
            }
        }

        // 合伙人
        if ($user->level < UserLevel::$keys['svip1']) {
            if ($count >= $set->svip1) {
                $user->level = UserLevel::$keys['svip1'];
                $user->save();
            }
        }

        // 合伙人 升 分公司
        if ($user->level == UserLevel::$keys['svip1']) {
            $c = User::find()
                ->where(['>=', 'level', UserLevel::$keys['svip1']])
                ->andWhere(['referrer_id' => $user->id])
                ->count();

            if ($c >= $set->svip2) {
                $user->level = UserLevel::$keys['svip2'];
                $user->save();
            }
        }
    }


    // public function __constructOld(Order $order)
    // {
    //     $this->order = $order;
    //     $this->user = User::findOne(['id' => $this->order->user_id]);
    //
    //     $first_order_item = OrderItem::find()
    //         ->where(['order_id' => $order->id])
    //         ->orderBy(['id' => SORT_ASC])
    //         ->limit(1)
    //         ->one();
    //
    //     $this->first_order_item = $first_order_item;
    //
    //     $product_id = $first_order_item->product_id;
    //
    //     $this->set = SetUserUp::getLastOne();
    //
    //     $is_bao = ProductArea::checkBao($product_id);
    //     $is_presale = ProductArea::checkPresale($product_id);
    //
    //     // 必须是报单区
    //     if ($is_bao) {
    //         $this->upMyself();
    //         $this->upChain();
    //     }
    //
    //     // 预售这个升级不用了
    //     // if ($is_presale) {
    //     //     if ($this->order->active_ok == 1) {
    //     //         $this->upMyself(true);
    //     //         $this->upChain();
    //     //     }
    //     // }
    //
    //     // 处理关系链上的待返(升完级后就处理了)
    //     $this->awaitCommission();
    //
    // }

    // protected function awaitCommission()
    // {
    //     $chain = Chain::getReferrerChain($this->user->id, [
    //         UserLevel::$keys['vip1'],
    //         UserLevel::$keys['vip2'],
    //         UserLevel::$keys['vip3'],
    //         UserLevel::$keys['vip4']
    //     ], true);
    //
    //     foreach ($chain as $user) {
    //         $has = Award::hasHelp($user->id);
    //         if ($has) {
    //             $this->saveCommissionAwait($user->id);
    //         }
    //     }
    // }

    // 升级自己
    // protected function upMyself($active = false)
    // {
    //     if ($active || $this->first_order_item->price >= $this->set->vip1) {
    //         if ($this->user->level < UserLevel::$keys['vip1']) {
    //             $this->user->level = UserLevel::$keys['vip1'];
    //             $this->user->save();
    //         }
    //     }
    //
    // }

    // 升级关系链
    // protected function upChain()
    // {
    //     $chain = Chain::getReferrerChain($this->user->id, [
    //         UserLevel::$keys['vip1'],
    //         UserLevel::$keys['vip2'],
    //         UserLevel::$keys['vip3'],
    //         UserLevel::$keys['vip4']
    //     ], true);
    //
    //     foreach ($chain as $user) {
    //         if ($user->level == UserLevel::$keys['vip1']) {
    //             $vip1 = Chain::getChildrenLevel($user->id, [
    //                 UserLevel::$keys['vip1'],
    //                 UserLevel::$keys['vip2'],
    //                 UserLevel::$keys['vip3'],
    //                 UserLevel::$keys['vip4']
    //             ]);
    //             if ($vip1 && count($vip1) >= $this->set->vip2) {
    //                 $user->level = UserLevel::$keys['vip2'];
    //                 $this->saveUser($user);
    //
    //                 // 处理20%待返
    //                 // $this->saveCommissionAwait($user->id);
    //             }
    //         }
    //         if ($user->level == UserLevel::$keys['vip2']) {
    //             $vip2 = Chain::getChildrenLevel($user->id, [
    //                 UserLevel::$keys['vip1'],
    //                 UserLevel::$keys['vip2'],
    //                 UserLevel::$keys['vip3'],
    //                 UserLevel::$keys['vip4']
    //             ]);
    //             $has_vip2 = Chain::getChildrenLevel($user->id, [
    //                 // UserLevel::$keys['vip1'],
    //                 UserLevel::$keys['vip2'],
    //                 UserLevel::$keys['vip3'],
    //                 UserLevel::$keys['vip4']
    //             ]);
    //
    //             if ($vip2 && $has_vip2 && count($has_vip2) >= $this->set->vip3) {
    //
    //                 // 判断前2个是不是创1
    //                 if (count($vip2) >= 2
    //                     && $vip2[0]['level'] >= UserLevel::$keys['vip2']
    //                     && $vip2[1]['level'] >= UserLevel::$keys['vip2']) {
    //                     $user->level = UserLevel::$keys['vip3'];
    //                     $this->saveUser($user);
    //                 }
    //             }
    //         }
    //         if ($user->level == UserLevel::$keys['vip3']) {
    //             $vip3 = Chain::getChildrenLevel($user->id, [
    //                 UserLevel::$keys['vip1'],
    //                 UserLevel::$keys['vip2'],
    //                 UserLevel::$keys['vip3'],
    //                 UserLevel::$keys['vip4']
    //             ]);
    //             $has_vip3 = Chain::getChildrenLevel($user->id, [
    //                 // UserLevel::$keys['vip1'],
    //                 // UserLevel::$keys['vip2'],
    //                 UserLevel::$keys['vip3'],
    //                 UserLevel::$keys['vip4']
    //             ]);
    //             if ($vip3 && $has_vip3 && count($has_vip3) >= $this->set->vip4) {
    //
    //                 // 判断前2个是不是创2
    //                 if (count($vip3) >= 2
    //                     && $vip3[0]['level'] >= UserLevel::$keys['vip3']
    //                     && $vip3[1]['level'] >= UserLevel::$keys['vip3']) {
    //                     $user->level = UserLevel::$keys['vip4'];
    //                     $this->saveUser($user);
    //                 }
    //             }
    //         }
    //     }
    // }

    // 每个人升级, 都要处理待返
    // private function saveUser(User $user)
    // {
    //     $user->save();
    //
    //     // 条件成立
    //     $has = Award::hasHelp($user->id);
    //     if ($has) {
    //         $this->saveCommissionAwait($user->id);
    //     }
    // }

    // protected function saveCommissionAwait($user_id)
    // {
    //     // 取出待返
    //     $commissions = UserCommission::find()
    //         ->where(['user_id' => $user_id])
    //         ->andWhere(['status' => 0])
    //         // ->andWhere(['award_type' => 201])
    //         ->all();
    //
    //     $account = UserAccount::findOne(['user_id' => $user_id]);
    //
    //     foreach ($commissions as $commission) {
    //         $commission->status = 1;
    //         $commission->save();
    //
    //         $account->balance += $commission->balance;
    //         $account->await_balance -= $commission->balance;
    //         $account->save();
    //     }
    // }

}